﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; //for DataTable
using System.Data.Sql;
using SD.LLBLGen.Pro.ORMSupportClasses;
using SD.LLBLGen.Pro.ORMSupportClasses.SelfServicingSpecific;
using SD.LLBLGen.Pro.DQE.SqlServer;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;


namespace MyHealthOnline
{
    public partial class ModifySpecialist : System.Web.UI.Page
    {
        WebServiceHO wbServ;
        
        DataRow dataRow;
        DataTable dt ;
        DropDownList ddlist;
        protected void Page_Load(object sender, EventArgs e)
        {
            
                wbServ = new WebServiceHO();
                //ddlist = empsGrid.FindControl("ddlEditDiscipline") as DropDownList;
                dt = new DataTable();

                string[] columns = { "Id", "Active", "DisciplineId", "Name", "Birthday", "Sex", "Email", "Password", "Phone", "Address", "City", "Country" };
                foreach (string elem in columns)
                {
                    if (elem.Equals("Active"))
                    {
                        dt.Columns.Add("Active", typeof(System.Boolean));
                    }
                    else
                    {
                        dt.Columns.Add(elem);
                    }
                }
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

                
                searchByName();
                empsGrid.DataSource = dt;
                empsGrid.DataBind();
                
        }

        protected void Discipline_DS_PerformSelect(object sender, PerformSelectEventArgs e)
        {
            // call GetMulti() on the contained collection to fetch the collection
            // Sort Fetched Categories by CategoryName.
            e.ContainedCollection.GetMulti(e.Filter, e.MaxNumberOfItemsToReturn, new SortExpression(DisciplineFields.Id | SortOperator.Ascending), e.Relations,  e.PrefetchPath);
            
            // fetch all customers using the information passed in via the 
            // PerformSelectEventArgs object. This select doesn't have to perform any paging, as the
            // data is for a combo box. 
            //e.ContainedCollection.GetMulti(e.Filter, e.MaxNumberOfItemsToReturn, e.Sorter,e.Relations, e.PrefetchPath);
            // If there are no categories, go to the Insert Mode of the FormView
            //if (e.ContainedCollection.Count <= 0)
            //{
            //    Message.Text = "No categories exist, start inserting categories.";
            //    CategoriesFormView.ChangeMode(FormViewMode.Insert);
            //}
        }

        protected void searchByName()
        {
            SpecialistCollection SP = new SpecialistCollection();
            SP.GetMulti(null); //fetch specialist data 
           
            string numeTextBox= nameTB.Text.ToString();
            
            foreach (SpecialistEntity spE in SP)
            {
                string[] name = spE.Name.Split(' ');
                if (name.Length == 2)
                {
                    string fname = name[0];
                    System.Diagnostics.Debug.WriteLine("First name is :" + fname);
                    string lname = name[1];

                    if (numeTextBox.Equals(spE.Name.ToString()) || (numeTextBox.Equals(fname)) || (numeTextBox.Equals(lname)))
                    {
                        string idSp = spE.Id.ToString();
                        dataRow = dt.NewRow();
                        dataRow["Id"] = spE.Id;
                        //if (spE.Active == 0)
                        //{
                        //    dataRow["Active"] = false;
                        //}
                        //else
                        //{
                        //    dataRow["Active"] = true;
                        //}
                        dataRow["DisciplineId"] = spE.DisciplineId;
                        dataRow["Name"] = spE.Name;
                        dataRow["Birthday"] = spE.Birthday;
                        dataRow["Sex"] = spE.Sex;
                        dataRow["Email"] = spE.Email;
                        dataRow["Password"] = spE.Password;
                        dataRow["Phone"] = spE.Phone;
                        dataRow["Address"] = spE.Address;
                        dataRow["City"] = spE.City;
                        dataRow["Country"] = spE.Country;
                        dt.Rows.Add(dataRow);
                    }
                }//end if(length..)
            }
        }


        protected void ddlDiscipline_OnInit(object sender, EventArgs e)
        {
            DisciplineCollection disciplines = new DisciplineCollection();
            SortExpression sorter = new SortExpression();
            sorter.Add(DisciplineFields.Id | SortOperator.Ascending);
            disciplines.GetMulti(null,0, sorter);
            //ExcludeIncludeFieldsList exclude = new ExcludeIncludeFieldsList(DisciplineFields);
            
            //disciplineDDL.DataSource = disciplines;
            //disciplineDDL.DataBind();
            
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
                empsGrid.EditIndex = e.NewEditIndex;
                //DropDownList DDLdiscipline = new DropDownList();
                //empsGrid.Rows[e.NewEditIndex].Cells[3].ColumnSpan = 2;
                GridViewRow Gvrow = empsGrid.Rows[e.NewEditIndex];
                DropDownList ddlist = Gvrow.FindControl("ddlEditDiscipline") as DropDownList;
                //empsGrid.Rows[e.NewEditIndex].Cells[3].Controls.Add(ddlist);
                if (ddlist != null)
                {
                    DisciplineCollection disciplines = new DisciplineCollection();
                    SortExpression sorter = new SortExpression();
                    sorter.Add(DisciplineFields.Id | SortOperator.Ascending);
                    disciplines.GetMulti(null,0, sorter);
                    ddlist.DataSource = disciplines;
                    ddlist.DataBind();
                }

                // DropDownList dropDownList2 = new DropDownList();// empsGrid.Controls.Add;// (DropDownList)empsGrid.FindControl("dropDownList2");
                //empsGrid.Rows[e.NewEditIndex].Cells[3].Controls.Add(DDLdiscipline);
                //DDLdiscipline.ID = "disciplineDDL";
                //ListItem lista = new ListItem();
                //lista.Value = "DD1";
                //lista.Value = "DD2";
                //DDLdiscipline.Items.Add(lista);

                //DDLdiscipline.AutoPostBack = true;
                empsGrid.DataSource = dt;
                empsGrid.DataBind();
            
        }
        protected void GridView1_RowCancelingEditing(object sender, GridViewCancelEditEventArgs e)
        {
            empsGrid.EditIndex = -1; // turn to edit mode; //swicth back to default mode
            empsGrid.DataSource = dt;
            empsGrid.DataBind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //Accessing Edited values from the GridView
             int i = e.RowIndex;
             GridViewRow row = (GridViewRow)empsGrid.Rows[i];
             string id = ((TextBox)empsGrid.Rows[i].Cells[1].Controls[0]).Text;
             int active;
             if (((CheckBox)empsGrid.Rows[i].Cells[2].Controls[0]).Checked)
             {
                 active = 1;
             }
             else
             {
                 active = 0;
             }
             int disciplineId = Convert.ToInt32( ((DropDownList)empsGrid.Rows[i].Cells[3].Controls[0]).SelectedValue);
             System.Diagnostics.Debug.WriteLine("IdDisciplina: "+disciplineId);
             string name = ((TextBox)empsGrid.Rows[i].Cells[4].Controls[0]).Text;
             string birthday = ((TextBox)empsGrid.Rows[i].Cells[5].Controls[0]).Text;
             string sex = ((TextBox)empsGrid.Rows[i].Cells[6].Controls[0]).Text;
             string email = ((TextBox)empsGrid.Rows[i].Cells[7].Controls[0]).Text; ;
             string password = ((TextBox)empsGrid.Rows[i].Cells[8].Controls[0]).Text;
             string phone = ((TextBox)empsGrid.Rows[i].Cells[9].Controls[0]).Text;
             string address = ((TextBox)empsGrid.Rows[i].Cells[10].Controls[0]).Text;
             string city = ((TextBox)empsGrid.Rows[i].Cells[11].Controls[0]).Text;
             string country = ((TextBox)empsGrid.Rows[i].Cells[12].Controls[0]).Text;

            
            //UPDATE ENTITY
             try
             {
                 SpecialistEntity specialist = new SpecialistEntity(id);  //find specilait with Id=id
                 //specialist.Active = active;
                 specialist.DisciplineId = disciplineId;
                 specialist.Name = name;
                 specialist.Birthday = birthday;
                 specialist.Sex = sex;
                 specialist.Email = email;
                 specialist.Password = password;
                 specialist.Phone = phone;
                 specialist.Address = address;
                 specialist.City = city;
                 specialist.Country = country;

                 //save it
                 specialist.Save();
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             
            //    // UpdateOrAddNewRecord(id, company, name, title, address, country, true); // call update method
            //    user_bll.editRow(nume, datanasterii, mail, username, parola, longitudine, latitudine, tip_utilizator);
            //    empsGrid.EditIndex = -1;
            //    BindData(); // Rebind GridView to reflect changes made
        }
        //protected void UpdateSelected(string id, int active, string disciplineId,string name,string birthday,string sex, string mail,string password,)
        //{
            
        //}

        //void BindData()
        //{
        //    empsGrid.DataSource = user_bll.getUsers();
        //    empsGrid.DataBind();
        //}
        //protected void GridView1_RowCancelingEditing(object sender, GridViewCancelEditEventArgs e)
        //{
        //    empsGrid.EditIndex = -1; // turn to edit mode
        //    BindData(); // Rebind GridView to show the data in edit mode
        //}
        //protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    empsGrid.EditIndex = e.NewEditIndex;
        //    BindData();

        //}
        //protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    //Accessing Edited values from the GridView
        //    GridViewRow row = (GridViewRow)empsGrid.Rows[e.RowIndex];


        //    string nume = ((TextBox)empsGrid.Rows[e.RowIndex].Cells[5].Controls[0]).Text; //ID
        //    string datanasterii = (((TextBox)empsGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text); //Company
        //    string mail = ((TextBox)empsGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text; //Name
        //    string username = ((TextBox)empsGrid.Rows[e.RowIndex].Cells[4].Controls[0]).Text; //Title
        //    string parola = ((TextBox)empsGrid.Rows[e.RowIndex].Cells[5].Controls[0]).Text; //Address
        //    int longitudine = Convert.ToInt32(((TextBox)empsGrid.Rows[e.RowIndex].Cells[6].Controls[0]).Text); //Country
        //    int latitudine = Convert.ToInt32(((TextBox)empsGrid.Rows[e.RowIndex].Cells[7].Controls[0]).Text); //Country
        //    int tip_utilizator = Convert.ToInt32(((TextBox)empsGrid.Rows[e.RowIndex].Cells[8].Controls[0]).Text); //Country
           
        //    // UpdateOrAddNewRecord(id, company, name, title, address, country, true); // call update method
        //    user_bll.editRow(nume, datanasterii, mail, username, parola, longitudine, latitudine, tip_utilizator);
        //    empsGrid.EditIndex = -1;
        //    BindData(); // Rebind GridView to reflect changes made
        //}

    }
}